﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNA_Game_Framework
{
	public static class AutoSizeTools
	{
		static Vector2 size;
		static Theme.Border border;
		static Texture2D background;

		public static Vector2 AdjustSizeToTextures(Vector2 controlSize, Theme.Border borderTheme, Texture2D backgroundTexture)
		{
			size = controlSize;
			border = borderTheme;
			background = backgroundTexture;

			AdjustWidthToTextures();
			AdjustHeightToTextures();

			return size;
		}

		private static void AdjustWidthToTextures()
		{
			int newWidth = (int)size.X;

			int topwidthnocorners = newWidth - border.bordertopleft.Width - border.bordertopright.Width;
			int bottomwidthnocorners = newWidth - border.borderbottomleft.Width - border.borderbottomright.Width;
			int backgroundnoborders = newWidth - border.borderleft.Width - border.borderright.Width;

			if (topwidthnocorners % border.bordertop.Width != 0 || bottomwidthnocorners % border.borderbottom.Width != 0 || backgroundnoborders % background.Width != 0)
			{
				int currentAmountOfWholeSlices = (backgroundnoborders) / background.Width;
				int newAmountOfSlices = currentAmountOfWholeSlices;

				int tempNewWidth = 0;
				while (tempNewWidth != newWidth)
				{
					newAmountOfSlices++;
					tempNewWidth = newAmountOfSlices * background.Width + border.borderleft.Width + border.borderright.Width;

					int tempNewWidthNoTopCorner = tempNewWidth - border.bordertopleft.Width - border.bordertopright.Width;
					int tempNewWidthNoBottomCorner = tempNewWidth - border.borderbottomleft.Width - border.borderbottomright.Width;
					int tempNewWidthNoBorders = tempNewWidth - border.borderleft.Width - border.borderright.Width;

					if (tempNewWidthNoTopCorner % border.bordertop.Width == 0 && tempNewWidthNoBottomCorner % border.borderbottom.Width == 0 && tempNewWidthNoBorders % background.Width == 0)
					{
						newWidth = tempNewWidth;
					}
				}
			}

			size.X = newWidth;
		}

		private static void AdjustHeightToTextures()
		{
			int newHeight = (int)size.Y;

			int leftheightnocorners = newHeight - border.bordertopleft.Height - border.borderbottomleft.Height;
			int rightheightnocorners = newHeight - border.bordertopright.Height - border.borderbottomright.Height;
			int backgroundnoborders = newHeight - border.bordertop.Height - border.borderbottom.Height;

			if (leftheightnocorners % border.borderleft.Height != 0 || rightheightnocorners % border.borderright.Height != 0 || backgroundnoborders % background.Height != 0)
			{
				int currentAmountOfWholeSlices = (backgroundnoborders) / background.Height;
				int newAmountOfSlices = currentAmountOfWholeSlices;

				int tempNewHeight = 0;
				while (tempNewHeight != newHeight)
				{
					newAmountOfSlices++;
					tempNewHeight = newAmountOfSlices * background.Height + border.bordertop.Height + border.borderbottom.Height;

					int tempNewHeightNoLeftCorner = tempNewHeight - border.bordertopleft.Height - border.borderbottomleft.Height;
					int tempNewHeightNoRightCorner = tempNewHeight - border.bordertopright.Height - border.borderbottomright.Height;
					int tempNewHeightNoBorders = tempNewHeight - border.bordertop.Height - border.borderbottom.Height;

					if (tempNewHeightNoLeftCorner % border.borderleft.Height == 0 || tempNewHeightNoRightCorner % border.borderright.Height == 0 || tempNewHeightNoBorders % background.Height == 0)
					{
						newHeight = tempNewHeight;
					}
				}
			}

			size.Y = newHeight;
		}
	}
}
